package com.example.suuu.project.system.controller;

import com.example.suuu.model.result.ResultVo;
import com.example.suuu.project.system.entity.SuMenu;
import com.example.suuu.project.system.entity.vo.UserLogin;
import com.example.suuu.project.system.service.impl.SuMenuServiceImpl;
import com.example.suuu.project.system.service.impl.SuUserServiceImpl;
import com.example.suuu.tools.log.LogPrint;
import com.example.suuu.tools.security.SecurityUtils;
import com.example.suuu.tools.tree.TreeUtils;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@CrossOrigin
@RestController
@RequestMapping("/login")
@Api(value = "/login", tags = {"登录 API接口"})
@ApiSupport(author = "sun")
public class SuLoginController {
    @Resource
    public SuUserServiceImpl suUserService;
    @Resource
    public SuMenuServiceImpl suMenuService;


    @PostMapping
    @LogPrint("登录")
    @ApiOperation(value = "登录", notes = "备注")
    public ResultVo login(@Validated @RequestBody UserLogin userLogin) {
        return new ResultVo(suUserService.login(userLogin.getUsername(), userLogin.getPassword()));
    }

    @GetMapping("/loginuser")
    @LogPrint("登录用户")
    @ApiOperation(value = "登录用户", notes = "登录用户")
    public ResultVo loginuser() {
        return new ResultVo(SecurityUtils.LoginUsers());
    }

    @GetMapping("/loginPermissions")
    @LogPrint("登录用户权限")
    @ApiOperation(value = "登录用户权限", notes = "登录用户权限")
    public ResultVo loginPermissions() {
        return new ResultVo(SecurityUtils.Permissions());
    }

    @GetMapping("/loginmc")
    @LogPrint("登录用户菜单")
    @ApiOperation(value = "登录用户菜单", notes = "登录用户菜单")
    public ResultVo loginmc() {
        List<SuMenu> list;
        if (SecurityUtils.LoginUsers().getUsercode().equals("999")) {
            SuMenu iu = new SuMenu();
            iu.setStatus(1);
            iu.setIsdel(1);
            list = suMenuService.getList(iu);
        } else {
            list = suMenuService.getByUserCode(SecurityUtils.LoginUsers().getUsercode());
        }
        List<Integer> mt = new ArrayList<Integer>();
        mt.add(1);
        mt.add(2);
        list = list.stream()
                .filter((SuMenu s) -> mt.contains(s.getMenutype()))
                .collect(Collectors.toList());
        return new ResultVo(TreeUtils.generateTrees(list));
    }
    @GetMapping("/loginmr")
    @LogPrint("登录用户路由")
    @ApiOperation(value = "登录用户路由", notes = "登录用户路由")
    public ResultVo loginmr() {
        List<SuMenu> list;
        if (SecurityUtils.LoginUsers().getUsercode().equals("999")) {
            SuMenu iu = new SuMenu();
            iu.setStatus(1);
            iu.setIsdel(1);
            list = suMenuService.getList(iu);
        } else {
            list = suMenuService.getByUserCode(SecurityUtils.LoginUsers().getUsercode());
        }
        return new ResultVo(list);
    }
}
